/*
 * @Author: FredWang
 * @Date:   2018-12-19 13:44:19
 * @Last Modified by:   FredWang
 */
import axios from "axios";
import { Message, MessageBox} from "element-ui";
import { ERR_OK } from "./config";

const axiosPost = function(url, data) {
  return new Promise((resolve, reject) => {
    axios
      .post(url, data)
      .then(res => {
        if (res && res.code === ERR_OK) {
          resolve(res);
        } else if (res && res.code === 2000) {
            MessageBox.alert('<div class="message-tip-biaoti"><span class="iconfont icon-info"></span><span class="message-title">提示</span></div><span class="message-cont">' + res.msg + '</span>', '', {
              confirmButtonText: '确定',
              showClose: false,
              dangerouslyUseHTMLString: true
            })
          resolve(res);
        } else {
          //如果服务端响应失败，则统一返回false
          Message({
            message: res.msg,
            type: "error"
          });
          return false;
        }
      })
      .catch(error => {
        reject(error);
      });
  });
};

export default axiosPost;
